package cn.tj.wenda.cloud.bas.rest;

import cn.tj.wenda.cloud.bas.entity.BasTenant;
import cn.tj.wenda.cloud.client.FallbackProxy;
import cn.tj.wenda.cloud.entity.ResponseBean;
import feign.hystrix.FallbackFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

@FeignClient(name="bas-core",path = "/api",fallbackFactory = BasTenantRestFuse.class)
@RequestMapping(value = "/bas/tenant")
@Api(tags="系统租户管理")
public interface BasTenantRest {

    @ApiOperation(value="取得所有租户一览",notes="")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="page",value="分页索引",required=false,dataType="Integer"),
    	@ApiImplicitParam(name="limit",value="返回记录数，为0时返回所有记录",required=false,dataType="Integer"),
    	@ApiImplicitParam(name="params",value="检索条件（JSON格式）",required=false,dataType="String")
    })
    @GetMapping("")
    public ResponseBean getBasTenantList(@RequestParam("page") int page, @RequestParam("limit") int limit,
                                         @RequestParam("params") String params);

    @ApiOperation(value="更新一条租户数据",notes="根据url的id来指定更新对象，并根据传过来的basTenant信息来更新租户详细信息")
    @ApiImplicitParams({            
    	@ApiImplicitParam(name="tenantid",value="租户ID",required=true,dataType="String"),
    	@ApiImplicitParam(name="basTenant",value="租户实体",required=true,dataType="BasTenant")
    })
    @PutMapping("/{tenantid}")
    public ResponseBean updateBasTenant(@PathVariable("tenantid") String tenantid,@RequestBody BasTenant basTenant);

    @ApiOperation(value="添加一条租户数据",notes="根据BasTenant对象创建租户")
    @ApiImplicitParam(name="basTenant",value="租户详细实体basTenant",required=true,dataType="BasTenant")
    @PostMapping("")
    public ResponseBean insertBasTenant(@RequestBody BasTenant basTenant);

    @ApiOperation(value="删除一条租户数据",notes="根据url的tenantid来指定删除对象")
    @ApiImplicitParam(name="tenantid",value="租户ID",required=true,dataType="String")
    @DeleteMapping("/{tenantid}")
    public ResponseBean deleteBasTenant(@PathVariable("tenantid") String tenantid);

}

/**
 * 中断工厂类
 */
@Component
class BasTenantRestFuse implements FallbackFactory<BasTenantRest> {

    private String microName = "bas-core";

    @Override
    public BasTenantRest create(Throwable cause) {
        BasTenantRest client = (BasTenantRest) FallbackProxy.newInstance(microName,BasTenantRest.class,cause);
        return client;
    }
}
